﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using System.Text.RegularExpressions;

namespace DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects
{
    public class SecurityRolesPage : WatiNBase
    {
        #region Constructors

        public SecurityRolesPage(WatiNBase watinBase) : base(watinBase.IEInstance, watinBase.SiteUrl, watinBase.DBName) { }

        public SecurityRolesPage(IE ieInstance, string siteUrl, string dbName) : base(ieInstance, siteUrl, dbName) { }

        #endregion

        #region Public Properties

        #region Tables
        public Table RolesTable
        {
            get { return IEInstance.Table(Find.ById(s => s.EndsWith("Roles_grdRoles"))); }
        }
        #endregion

        #region Links
        public Link AddNewRoleLink
        {
            get { return IEInstance.Link(Find.ByTitle("Add New Role")); }
        }
        public Link AddNewRoleGroupLink
        {
            get { return IEInstance.Link(Find.ByText(s => s.Contains("Add New Role Group"))); }
        }
        public Link UpdateLink
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.Link(Find.ByTitle("Update"));
                }
                return IEInstance.Link(Find.ByTitle("Update"));
            }
        }
        public Link AddUserToRoleLink
        {
            get { return ContentDiv.Div(Find.ById(s => s.EndsWith("SecurityRoles_pnlRoles"))).Link(Find.ByTitle("Add User to Role")); }
        }
        #endregion

        #region TextFields
        public TextField RoleNameField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("EditRoles_txtRoleName")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("EditRoles_txtRoleName")));
            }
        }
        public TextField RoleDescription
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("EditRoles_txtDescription")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("EditRoles_txtDescription")));
            }
        }
        public TextField RoleGroupNameField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("EditGroups_txtRoleGroupName")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("EditGroups_txtRoleGroupName")));
            }
        }
        public TextField RoleGroupDescriptionField
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.TextField(Find.ById(s => s.EndsWith("EditGroups_txtDescription")));
                }
                return IEInstance.TextField(Find.ById(s => s.EndsWith("EditGroups_txtDescription")));
            }
        }
        #endregion

        #region SelectLists
        public SelectList RoleGroupSelectList
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.SelectList(Find.ById(s => s.EndsWith("EditRoles_cboRoleGroups")));
                }
                return IEInstance.SelectList(Find.ById(s => s.EndsWith("EditRoles_cboRoleGroups")));
            }
        }
        public SelectList FilterByGroupSelectList
        {
            get { return IEInstance.SelectList(Find.ById(s => s.EndsWith("Roles_cboRoleGroups"))); }
        }
        public SelectList AddUserToRoleSelectList
        {
            get { return ContentDiv.SelectList(Find.ById(s => s.EndsWith("SecurityRoles_cboUsers"))); }
        }
        #endregion

        #region CheckBoxes
        public CheckBox PublicRoleCheckBox
        {
            get
            {
                if(PopUpFrame != null)
                {
                    return PopUpFrame.CheckBox(Find.ById(s => s.EndsWith("EditRoles_chkIsPublic")));
                }
                return IEInstance.CheckBox(Find.ById(s => s.EndsWith("EditRoles_chkIsPublic")));
            }
        }
        public CheckBox AutoAssignCheckBox
        {
            get
            {
                if (PopUpFrame != null)
                {
                    return PopUpFrame.CheckBox(Find.ById(s => s.EndsWith("EditRoles_chkAutoAssignment")));
                }
                return IEInstance.CheckBox(Find.ById(s => s.EndsWith("EditRoles_chkAutoAssignment")));
            }
        }
        #endregion

        #region Images
        public Image DeleteRoleGroupButton
        {
            get { return IEInstance.Image(Find.ById(s => s.EndsWith("Roles_cmdDelete"))); }
        }
        public Image EditRoleGroupButton
        {
            get { return IEInstance.Image(Find.ById(s => s.EndsWith("Roles_imgEditGroup"))); }
        }
        #endregion

        #endregion

        #region Public Methods

        public Link GetUserLinkFromUserRolesPage(string userName)
        {
            return IEInstance.Link(Find.ByText(userName));
        }


        public void AddRoleGroup(string groupName)
        {
            AddNewRoleGroupLink.Click();
            RoleGroupNameField.Value = groupName;
            UpdateLink.Click();
            System.Threading.Thread.Sleep(1000);
        }

        public void AddRole(string roleName)
        {
            AddNewRoleLink.Click();
            RoleNameField.Value = roleName;
            UpdateLink.ClickNoWait();
            System.Threading.Thread.Sleep(1500);
        }

        public void AddUserToRole(string roleName, string userDisplayName)
        {
            TableRow roleRow = GetRoleRow(roleName);
            roleRow.Image(Find.ByTitle("Manage Users")).Click();

            AddUserToRoleSelectList.Select(new Regex(userDisplayName + "(.*)"));

            System.Threading.Thread.Sleep(1000);
            AddUserToRoleLink.Click();
        }

        public virtual Image GetManageUsersButton(string roleName)
        {
            //Returns the Edit Button for the user specified 
            Image result = null;
            foreach (TableRow row in RolesTable.TableRows)
            {
                if (row.TableCells[2].InnerHtml.Contains(roleName))
                {
                    result = row.Image(Find.ByTitle("Manage Users"));
                    break;
                }
                continue;
            }
            return result;
        }


        public virtual Image GetRoleEditButton(string roleName)
        {
            //Returns the Edit Button for the user specified 
            Image result = null;
            foreach (TableRow row in RolesTable.TableRows)
            {
                if (row.TableCells[2].InnerHtml.Contains(roleName))
                {
                    result = row.Image(Find.ByTitle("Edit"));
                    break;
                }
                continue;
            }
            return result;
        }


        public virtual TableRow GetRoleRow(string roleName)
        {
            //Returns the user Row specified from the user table
            TableRow result = null;
            foreach (TableRow row in RolesTable.TableRows)
            {
                if (row.TableCells[2].InnerHtml.Contains(roleName))
                {
                    result = row;
                    break;
                }
                continue;
            }
            return result;
        }

        #endregion
    }
}
